package com.test.contract;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * Anchor : Create by CimZzz
 * Time : 2020/06/07 14:54:16
 * Project : contract
 * Since Version : Alpha
 */
public class AddActivity extends AppCompatActivity {
    EditText nameTxt;
    EditText workTxt;
    EditText phoneTxt;
    EditText qqTxt;
    EditText addressTxt;

    User selectUser;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        findViewById(R.id.container).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View focus = getCurrentFocus();
                if(focus != null) {
                    InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    manager.hideSoftInputFromWindow(focus.getWindowToken(), 0);
                    focus.clearFocus();
                }
            }
        });
        nameTxt = findViewById(R.id.nameTxt);
        workTxt = findViewById(R.id.workTxt);
        phoneTxt = findViewById(R.id.phoneTxt);
        qqTxt = findViewById(R.id.qqTxt);
        addressTxt = findViewById(R.id.addressTxt);

        findViewById(R.id.quitBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setResult(0);
                finish();
            }
        });

        findViewById(R.id.saveBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Context context = AddActivity.this;
                User user = selectUser == null ? new User() : selectUser;
                user.setName(nameTxt.getText().toString());
                if(TextUtils.isEmpty(user.getName())) {
                    ToastUtils.sendToast(context, "姓名不能为空");
                    return;
                }
                user.setWork(workTxt.getText().toString());
                if(TextUtils.isEmpty(user.getWork())) {
                    ToastUtils.sendToast(context, "单位不能为空");
                    return;
                }
                user.setPhone(phoneTxt.getText().toString());
                if(TextUtils.isEmpty(user.getPhone())) {
                    ToastUtils.sendToast(context, "手机号不能为空");
                    return;
                }
                if(!user.getPhone().matches("1[0-9]{10}")) {
                    ToastUtils.sendToast(context, "输入正确的手机号");
                    return;
                }
                user.setQq(qqTxt.getText().toString());
                if(TextUtils.isEmpty(user.getQq())) {
                    ToastUtils.sendToast(context, "QQ不能为空");
                    return;
                }
                user.setAddress(addressTxt.getText().toString());
                if(TextUtils.isEmpty(user.getAddress())) {
                    ToastUtils.sendToast(context, "地址不能为空");
                    return;
                }
                SqlHelper sqlHelper = new SqlHelper(context);
                if(selectUser != null) {
                    sqlHelper.update(user);
                }
                else {
                    sqlHelper.insert(user);
                }
                sqlHelper.close();
                Intent intent = new Intent();
                intent.putExtra("User", user);
                setResult(selectUser != null ? 1 : 2, intent);
                finish();
            }
        });

        if(getIntent() != null) {
            selectUser = (User) getIntent().getSerializableExtra("User");
            if (selectUser != null) {
                nameTxt.setText(selectUser.getName());
                workTxt.setText(selectUser.getWork());
                phoneTxt.setText(selectUser.getPhone());
                qqTxt.setText(selectUser.getQq());
                addressTxt.setText(selectUser.getAddress());
            }
        }
    }
}
